package com.atguigu.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Admin;
import com.atguigu.service.AdminService;
import com.atguigu.service.RoleService;
import com.atguigu.util.QiniuUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping(value = "/admin")
public class AdminController extends BaseController {

    @Reference
    private AdminService adminService;

    @Reference
    private RoleService roleService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @RequestMapping
    public String index(ModelMap model, HttpServletRequest request) {
        Map<String, Object> filters = getFilters(request);
        PageInfo<Admin> page = adminService.findPage(filters);

        model.addAttribute("page", page);
        model.addAttribute("filters", filters);
        return "admin/index";
    }

    @GetMapping("/create")
    public String create() {
        return "admin/create";
    }


    @PostMapping("/save")
    public String save(Admin admin) {
        // 将密码加密
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        // 设置默认头像
        admin.setHeadUrl("https://pic2.zhimg.com/v2-0877d48027319c6002163d215ca6d25e_xll.jpg");
        adminService.insert(admin);

        return "common/successPage";
    }


    @GetMapping("/edit/{id}")
    public String edit(ModelMap model, @PathVariable Long id) {
        Admin admin = adminService.getById(id);
        model.addAttribute("admin", admin);
        return "admin/edit";
    }

    @PostMapping(value = "/update")
    public String update(Admin admin) {

        adminService.update(admin);

        return "common/successPage";
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        adminService.delete(id);
        return "redirect:/admin";
    }

    @GetMapping("/uploadShow/{id}")
    public String uploadShow(ModelMap model, @PathVariable Long id) {
        model.addAttribute("id", id);
        return "admin/upload";
    }

    @PostMapping("/upload/{id}")
    public String upload(@PathVariable Long id, @RequestParam(value = "file") MultipartFile file, HttpServletRequest request) throws IOException {
        // UUID Name
        String newFileName = UUID.randomUUID().toString();
        // 上传图片
        QiniuUtil.upload2Qiniu(file.getBytes(), newFileName);
        Admin admin = new Admin();
        admin.setId(id);
        String url = QiniuUtil.url + newFileName;
        admin.setHeadUrl(url);
        adminService.update(admin);
        return "common/successPage";
    }

    // 权限-进入分配角色页面
    @GetMapping("/assignShow/{adminId}")
    public String toAssignShowPage(ModelMap modelMap,@PathVariable Long adminId) {
        // 带adminId（用户ID）
        modelMap.addAttribute("adminId", adminId);
        // 根据用户ID查询用户角色 （map中放list）
        Map<String, Object> roleMap = roleService.findRoleByAdminId(adminId);
        // 带 未选中角色 已选择角色 带过去；注：key必须要和前端获取的key一致
        modelMap.addAllAttributes(roleMap); //  all方便通过键值对的键取值
        return "admin/assignShow";
    }
    // 分配角色
    @RequestMapping("/assignRole")
    public String assignRole(Long adminId, Long[] roleIds){
        roleService.assignRole(adminId,roleIds);
        return "common/successPage";
    }
}
